import numpy as np  
from sklearn import datasets, svm, metrics  
from sklearn.model_selection import train_test_split  
from sklearn.preprocessing import StandardScaler  
  
# 1. 加载数据  
digits = datasets.load_digits()  
print(digits)
  
# 2. 数据预处理：将数据分为训练集和测试集，并标准化特征  
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42)  
scaler = StandardScaler()  
X_train = scaler.fit_transform(X_train)  
X_test = scaler.transform(X_test)  
  
# 3. 训练SVM分类器  
clf = svm.SVC(gamma=0.001, C=100.)  
clf.fit(X_train, y_train)  
  
# 4. 评估模型  
predicted = clf.predict(X_test)  
print("Classification report for classifier %s:\n%s\n"  
      % (clf, metrics.classification_report(y_test, predicted)))  
# print("Confusion matrix:\n%s" % metrics.confusion_matrix(y_test, predicted))